package genedit.controller.interfaceActions;

import genedit.controller.AddState;
import genedit.controller.RectangleCreator;
import genedit.model.Workspace;
import genedit.view.graph.GraphDocumentView;
import genedit.view.graph.GraphWorkArea;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.KeyStroke;

/**
 * Akcija za dodavanje pravougaonika na radnu površinu.
 * 
 * @author Nikola
 * @version 1.0 15/10/12
 */
@SuppressWarnings("serial")
public class GRectangleA extends AbstractAction {
	private static AbstractAction rectangleA = null;

	public GRectangleA() {
		putValue(NAME, "");
		putValue(SMALL_ICON, new ImageIcon("resources/icons/stock_draw_rectangle_unfilled.png"));
		putValue(MNEMONIC_KEY, KeyEvent.VK_R);
		putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_R, ActionEvent.CTRL_MASK));
		putValue(SHORT_DESCRIPTION, "Rectangle (Ctrl+R)");
	}

	public static AbstractAction getInstance() {
		if (rectangleA == null)
			rectangleA = new GRectangleA();
		return rectangleA;
	}

	/**
	 * Akcija postavlja globalno aktivno stanje na stanje dodavanja sa fabrikom
	 * za instanciranje MyRect grafičkih elemenata.
	 * 
	 * @see RectangleCreator, MyRect
	 */
	public void actionPerformed(ActionEvent event) {
		Workspace.getInstance().setActiveState(new AddState(null, new RectangleCreator()));

		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("resources/icons/cursorAdd1.png");
		Point hotSpot = new Point(0, 0);
		Cursor cursor = toolkit.createCustomCursor(image, hotSpot, "CursorProba");
		GraphDocumentView document = Workspace.getInstance().getActiveDocument();
		if (document != null) {
			GraphWorkArea test = ((GraphWorkArea) document.getWorkArea());

			test.setCursor(cursor);
			GraphWorkArea.GraphController test2 = test.getController();
			test2.setCurrentState(new AddState((GraphWorkArea) Workspace.getInstance()
					.getActiveDocument().getWorkArea(), new RectangleCreator()));
		}
	}
}